const http = require('http')
const fs = require('fs')

const wait = (seconds) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve()
        }, seconds * 1000)
    })
}

http.createServer(function(request, response) {
    console.log('requext come ', request.headers.host)

    if (request.url === '/') {
        const html = fs.readFileSync('test.html', 'utf-8')

        response.writeHead(200, {
            'Content-Type': 'text/html'
        })
        response.end(html)
    }

    //在这里声明一个接口，在html中进行访问
    if (request.url === '/data') {
        wait(2).then(() => response.end('success'))
    }
}).listen(8888)

console.log('当web服务器启动后，在控制显示提示信息：server listening on 8888  或者显示：服务已启动，请在浏览器中访问 http://localhost:8888/')